package org.Console;

import java.util.List;

import pasantias.Dominio.BoardRender;
import pasantias.Dominio.Circle;
import pasantias.Dominio.Dot;
import pasantias.Dominio.Figura;
import pasantias.Dominio.FiguresVisitor;
import pasantias.Dominio.InmutableBoard;
import pasantias.Dominio.Rectangle;
import pasantias.Dominio.Square;

public class Text implements BoardRender{
	
	private char map[][];
	
	@Override
	public void draw(InmutableBoard inmutableBoard) {
	
		List<Figura> figures = inmutableBoard.getFigures();
		addBoardFrame(inmutableBoard.getWidth(), inmutableBoard.getHeigh());
		
		FiguresVisitor visitor = new FiguresVisitor(){
			
			@Override
			public void visit(Circle circle) {
				add(circle);
			}
			@Override
			public void visit(Dot dot) {
				add(dot);
			}

			@Override
			public void visit(Rectangle rectangle) {
				add(rectangle);
			}

			@Override
			public void visit(Square square) {
				add(square);
			}}; 
		
		for (Figura figura : figures) {
			figura.accept(visitor);
		}
	}
	
	public void addBoardFrame(int width, int height){
		int widthFrame = width + 2;
		int heightFrame = height + 2;
		map = new char[heightFrame][widthFrame];
		int row, column;
		
		for(row=0; row < heightFrame; row++ ){
			for(column=0; column < widthFrame; column++){
				if(row == 0 || row == heightFrame-1){
					map[row][column] = '-';
				}else{
					if((row != 0 || row != heightFrame-1) && 
							(column == 0 || column == widthFrame-1)){
						map[row][column] = '|';
					}else{
						map[row][column] = ' ';
					}
				}
			}
		}
	}
	
	public void add(Rectangle rectangle){

		int rectangleEndX = rectangle.getVertex().getX() + rectangle.getWidth();
		int rectangleEndY = rectangle.getVertex().getY() + rectangle.getHeight();
		
		for(int column = rectangle.getVertex().getX(); column<rectangleEndX; column++){
			for (int row = rectangle.getVertex().getY(); row < rectangleEndY; row++){
				map[row][column] = '*';
			}
		}
	}
	
	public void add(Circle circle){

		int i, lines;
		
		if (circle.getRadius() % 3 == 0){
			i = (circle.getRadius() / 3);
		}else{
			i = (circle.getRadius() / 3) + 1;
		}
			
		lines = 3 * i - (i-1);
		
		map[circle.getCenter().getY()][circle.getCenter().getX()]='*';
		
		for(int j=1; j<=circle.getRadius(); j++){
			map[circle.getCenter().getY()][circle.getCenter().getX()-j]='*';
			map[circle.getCenter().getY()][circle.getCenter().getX()+j]='*';
		}
		
		for(int j=0; j<lines-i-1; j++){
			for(int k=1; k<(circle.getRadius()-j)*2; k++){
				
				map[circle.getCenter().getY()-j-1][circle.getCenter().getX()+k-circle.getRadius()+j]='*';
				map[circle.getCenter().getY()+j+1][circle.getCenter().getX()+k-circle.getRadius()+j]='*';
				
			}
		}
//		printMap();
	}

	public void add(Dot dot){
		
		map[dot.getY()][dot.getX()]='*';
//		printMap();
		
	}
	
	public String toString(int lineNumber){
		String result = "";
		for(int i = 0 ; i< map[0].length; i++){
			result += map[lineNumber][i];
		}
			
		return result;
	}
	
	public String printMap(){
		String result="";
		int row, column;
		for(row=0; row < map.length; row++ ){
			for(column=0; column < map[0].length; column++){
				System.out.print(map[row][column]);
				result+= map[row][column];
			}
			System.out.println();
			result+="\n";
		}
		return result;
	}

}
